<?php

class myclass {

     public $DBASE = "";
     public $CONN = "";
     public $_COUNT = "0";

     // ////////////////////////////////////////////////////
     //	***************************************************
     //	PHP and MySQL Connection and Error Specific methods
     //	***************************************************

     function myclass($server="", $dbase="", $user="", $pass="") {
          $this->DBASE = $dbase;
          //$conn = mysqli_connect($server,$user,$pass);
          $conn = mysql_connect($server, $user, $pass, $dbase);
          if (!$conn) {
               $this->error("Connection attempt failed");
          }
          $db_selected = mysql_select_db($dbase, $conn);
          
          if ($db_selected) {
               $this->error("Dbase Select failed");
          }
          $this->CONN = $conn;
          $this->sql_query("set time_zone = '+0:00' ");
          return true;
     }

     function select_database($dbase) {
          $conn = $this->CONN;
          $this->DBASE = $dbase;
           
          if (!mysql_select_db($dbase,$conn )) {
               $this->error("Dbase Select failed");
          }
     }

     function close() {
          $conn = $this->CONN;
          $close = mysql_close($this->CONN);
          unset($this->CONN);
          if (!$close) {
               $this->error("Connection close failed");
          }
          return true;
     }

     function error($text) {
     	return;
          #$conn = $this->CONN;
          #$no = mysql_errno($conn);
          #$msg = mysql_error($conn);
          exit;
     }

     function select($sql="", $fetch="mysql_fetch_assoc", $flag='No') {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          $results = mysql_query($sql,$conn);
          //write query in file
          #mysqli_next_result($conn);
          if ($flag == 'Yes')
               print_r($results);
          if ((!$results) or (empty($results))) {
               return false;
          }
          $count = 0;
          $data = array();
          while ($row = $fetch($results)) {
               $data[$count] = $row;
               $count++;
          }
          @mysql_free_result($results);
          return $data;
     }

     function selectfetch($sql="", $fetch="mysql_fetch_assoc", $assoc_fields='') {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          
          $results = mysql_query($sql,$conn);
          #mysqli_next_result($conn);
          if ((!$results) or (empty($results))) {
               return false;
          }
          $count = 0;
          $data = array();
          if ($fetch == "mysql_fetch_assoc") {
               if ($assoc_fields == '') {
                    while ($row = $fetch($results)) {
                         $data[$count] = $row;
                         $count++;
                    }
               } else {
                    while ($row = $fetch($results)) {
                         $data[$row[$assoc_fields]][] = $row;
                    }
               }
          } else {
               while ($row = $fetch($results)) {
                    $data[$count] = $row;
                    $count++;
               }
          }
          @mysql_free_result($results);
          return $data;
     }

     function execute($sql="", $fetch="mysql_fetch_assoc") {
          $this->_COUNT++;
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          
          $results = @mysql_query($sql,$conn);
          #$res1 = mysqli_next_result($conn);
          #mysqli_next_result($conn);
          if ((!$results) or (empty($results))) {
               return false;
          }
          $count = 0;
          $data = array();
          while ($row = $fetch($results)) {
               $data[$count] = $row;
               $count++;
          }
          mysql_free_result($results);
          return $data;
     }

     function affected($sql="") {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (!preg_match("/^select/", $sql)) {
               echo "wrongquery<br>$sql<p>";
               echo "<H2>Wrong function silly!</H2>\n";
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          $results = @mysql_query($sql,$conn);
          if ((!$results) or (empty($results))) {
               return false;
          }
          $tot = 0;
          $tot = mysql_affected_rows();
          return $tot;
     }

     function insert($sql="") {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          $results = mysql_query($sql,$conn);
          $this->writeQueryInFile('insert', $sql);
          #mysqli_next_result($conn);
          if (!$results) {
               $this->error("<H2>No results!</H2>\n");
               return false;
          }
          $id = mysql_insert_id($conn);
          return $id;
     }

     function sql_query($sql="", $fetch="mysql_fetch_assoc") {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          $results = mysql_query($sql,$conn) or die("query fail >> " . $sql . " <hr>");
          $this->writeQueryInFile('update', $sql);
          #mysqli_next_result($conn);
          if (!$results) {
               $message = "Query went bad!";
               $this->error($message);
               return false;
          }



          if (!preg_match("/^select/", $sql) && !preg_match("/^call/", $sql)) {
               return true;
          } else {
               $count = 0;
               $data = array();
               while ($row = @$fetch($results)) {
                    $data[$count] = $row;
                    $count++;
               }
               @mysql_free_result($results);
               return $data;
          }
     }

     // Call Below function to get the Results for a Query. It will return Two Dimesional Array.
     function select_assoc_array($sql="", $key='', $fetch=MYSQL_ASSOC) {
          $this->_COUNT++;
          if (empty($sql)) {
               return false;
          }
          if (!preg_match("/^select/", $sql) && !preg_match("/^call/", $sql)) {
               echo "wrongquery<br>$sql<p>";
               echo "<H2>Wrong function silly!</H2>\n";
               return false;
          }
          if (empty($this->CONN)) {
               return false;
          }
          $conn = $this->CONN;
          $results = @mysql_query($sql,$conn);
          if ((!$results) or (empty($results))) {
               return false;
          }
          $count = 0;
          $data = array();
          while ($row = mysql_fetch_array($results, $fetch)) {
               $data[$row[$key]][] = $row;
               $count++;
          }
          mysql_free_result($results);
          return $data;
     }


//ends the class over here
     function writeQueryInFile($query_type, $query) {
          /*
            global $site_path;
            $content = "<hr>";
            $content.='<table cellpadding="2" width="100%" cellpadding="2" border="1">';
            $content.='<tr><td width="13%">Query :</td><td>' . $query;
            $content.="</td></tr>";
            $content.="<tr><td>Date Time : </td><td>" . date('Y-m-d h:i:s');
            $content.="</td></tr>";
            $content.="<tr><td>HTTP Referrer : </td><td>" . $_SERVER['HTTP_REFERER'];
            $content.="</td></tr>";
            $content.="<tr><td>From IP : </td><td>" . $_SERVER['REMOTE_ADDR'];
            $content.="</td></tr>";
            $content.="</table>";
            $content.="<br>";
            $filename = $site_path . "lib/query/" . $query_type . "_" . date('Y_m_d') . ".html";
           * */
     }

}

?>
